<?php

class General_Model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function getAllDataToolAdmin($query) {
        $result = array();
        //$QUERY = $query . " " . $where;
        $resultSet = $this->db->query($query);
        $result['numRows'] = $resultSet->num_rows();
        $result['lastQuery'] = $this->db->last_query();
        return $result;
    }


    public function getScalar($colomn, $table, $where, $value) {
        $SQL = "SELECT " . $colomn . " FROM " . $table . " WHERE " . $where . "='" . $value . "'";
        $resultSet = $this->db->query($SQL);
        $result = $resultSet->row();
        $dtresult = $result->$colomn;
        return $dtresult;
    }

   

    public function checkDupKey($colom, $value, $table) {
        $SQL = "SELECT " . $colom . " FROM " . $table . " WHERE " . $colom . " = '" . $value . "'";
        $resultSet = $this->db->query($SQL);
        $result = $resultSet->row();

        if (count($result) < 1) {
            $out = false;
        } else {
            $out = true;
        }

        return $out;
    }

    public function checkDupKeyByConds($conds, $table) {
        $SQL = "SELECT * FROM " . $table . " WHERE " . $conds;
        $resultSet = $this->db->query($SQL);
        $result = $resultSet->row();

        if (count($result) < 1) {
            $out = false;
        } else {
            $out = true;
        }

        return $out;
    }

    public function writeLog($arrValsLog) {
        $this->db->insert('audit_trail', $arrValsLog);
    }

    public function last_index($seqName) {
        $sql = "SELECT currval('$seqName'::regclass) as newkode";
        $result = $this->db->query($sql);
        $row = $result->row();
        $newKode = $row->newkode;
        return $newKode;
    }

    public function next_index($seqName) {
        $sql = "SELECT nextval('$seqName'::regclass) as newkode";
        $result = $this->db->query($sql);
        $row = $result->row();
        $newKode = $row->newkode;
        return $newKode;
    }

    

}

?>
